#!/bin/python3

from optparse import OptionParser
import requests
import re
import time
import sys
import os
import threading

option = OptionParser()
option.add_option('-n','--num',action = 'store',type=int,dest = 'pingnum',default = 4, help = "这里是使用ping发送包的数量")
option.add_option('-u','--url',action = 'store',type=str,dest = 'url', help = "这里是要检查的url")
option.add_option('--noping',action = 'store_true',dest = 'noping', help = "这个参数存在就不发送ping包")
option.add_option('--history',action = 'store_true',dest = 'history', help = "这个参数存在就检查历史ip")

f = []
class MyThread(threading.Thread):
    global f
    def __init__(self,func,data,j,pingnum):
        threading.Thread.__init__(self)
        self.data=data
        self.j=j
        self.func=func
        self.pingnum = pingnum
    def run(self):
        while len(self.data)>0:
            self.sult = self.func(self.data.pop(),self.pingnum)
            f.append(self.sult)

class IP138(object):
    global f#url
    def __init__(self,op):
        self.url = op.url
        self.history = op.history
        self.noping = op.noping
        self.pingnum = op.pingnum
        self.session = requests.session()
        self.headers = {"User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:79.0) Gecko/20100101 Firefox/79.0"}
        self.session.headers = self.headers
        self.root_url = "https://site.ip138.com/{ip}/".format(ip = self.url)
        self.urlwrite1 = "https://site.ip138.com/domain/write.do?input={ip}&token={token}"
        self.urlread = "https://site.ip138.com/domain/read.do?domain={ip}&time={time}"
        self.urlwrite2 = "https://site.ip138.com/domain/write.do?type=domain&input={ip}&token={token}"
    
    def get_token1(self):
        try :
            r = self.session.get(self.root_url)
            r.raise_for_status()
            a = re.findall("var _TOKEN = '(.*?)';",r.text)[0]
            txt_list = re.findall("(?<=target=\"_blank\">)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?=</a>)",r.text)
            return a,txt_list
        except:
            return 0

    def send(self,token):#fayigekongbao
        try :
            r = self.session.get(self.urlwrite1.format(ip = self.url,token = token))
            r.raise_for_status()
            return 1
        except :
            return 0
    def get_read(self):
        try:
            timestr = str(time.time()).replace('.','')[0:13]
            r = self.session.get(self.urlread.format(ip=self.url,time = timestr))
            r.raise_for_status()
            return r.json()
        except:
            return 0

    def get_write2(self,token):
        try:
            r = self.session.get(self.urlwrite2.format(ip = self.urlwrite2,token = token))
            r.raise_for_status()
            return 1
        except:
            return 0

    def packet(self,ip,num):
        a = os.popen("ping -c {num} {ip}".format(ip = ip, num = int(num)),'r',-1)
        b = a.read()
        c = re.findall("received, (.*?) packet loss,",b)[0]#丢包率
        # c = re.findall("已接收.*?个包, (.*?) packet loss, time \d+ms",b)[0]
# '''
# packet loss, time 9033ms
# rtt min/avg/max/mdev = 145.546/171.560/199.640/19.594 ms
# '''
        d = b.split('\n')[-3].split(' ')[-1]
        print(ip,'\t',self.url,"#{}个包丢包率:{},时间:{}".format(num,c,d))

    def main(self):
        token1,txt = self.get_token1()
        #txt是历史ip列表
        if self.history:
            for i in txt:
                print("#{}\t{}".format(i,self.url))
        self.send(token1)
        a = self.get_read()
        # print(a['data'])
        print("#已经从服务器获取数据集")
        try:
            ip_list = [i['ip'] for i in a['data']]
            thread_list = []
            if self.noping :
                # print(123132)
                for i in ip_list:
                    print(i,'\t',self.url)
            else:
                for i in range(4):
                    t1 = MyThread(self.packet,ip_list,i,self.pingnum)
                    t1.start()
                    thread_list.append(t1)
                for i in thread_list:
                    i.join()
        except:
            print("域名异常,或程序异常")
        token2 = a['msg']
        self.get_write2(token2)

if __name__ == "__main__":
    op = option.parse_args(sys.argv)[0]
    # print(op)
    IP138(op).main()

#author=江户川_柯北
#version=0.5
#last change time=2021年3月20日21：41
#增加了多线程ping，本来想异步的但是没看懂